<#macro login_component>
<script>
    layui.use(['global', 'sha256.min', 'jquery.form.min', 'zTreeSelect'], function () {
        var $ = layui.$, zTreeSelect = layui.zTreeSelect;
        function clearSubmitBtnLoadingState() {
            var submitBtn = $('#btn_submit');
            if (submitBtn) {
                submitBtn.html('登录');
                submitBtn.removeAttr('disabled');
            }
        }
        function addSubmitBtnLoadingState() {
            var submitBtn = $('#btn_submit');
            if (submitBtn) {
                submitBtn.html('登录中 <i id="btn_submit_icon" ' +
                    'class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop inline_icon"></i>');
                submitBtn.attr('disabled', 'disabled');
            }
        }
        $('#loginForm').ajaxForm({
            data: {
                password: function () {
                    addSubmitBtnLoadingState();
                    var loginSeed = null;
                    $.ajax({
                        url :'/login/token',
                        data:{},
                        async : false,
                        success:function (data) {
                            if (data.success) {
                                loginSeed = data.data;
                            }
                        },
                        error: function() {
                            clearSubmitBtnLoadingState();
                            alert('服务器异常，请稍后再试。');
                        }
                    });
                    var password = sha256(sha256($('#password').val()) + loginSeed);
                    $(this).clearForm();
                    return password;
                },
                appCode: '${appCode!''}',
                targetUrl: '${targetUrl!'/'}'
            },
            success: function (data) {
                if ('${targetUrl!'/'}'.indexOf('?') >= 0) {
                    window.location.href = '${targetUrl!'/'}' + '&token_enc='+data.data;
                } else {
                    window.location.href = '${targetUrl!'/'}' + '?token_enc='+data.data;
                }
            },
            error:function(data) {
                clearSubmitBtnLoadingState();
            },
        });
        $('#btn-register').click(function () {
            layer.msg('请联系管理员注册', {
                time:2000
            });
        });
        zTreeSelect.render({
            treeDomId: 'menuContent',
            treeShowValueDomId: 'deptId',
            showMode: 'full',
            async: {
                enable: true,
                type: 'get',
                url:"/login/listSubDept",
                autoParam:["id=parentId"],
                dataFilter: function(treeId, parentNode, responseData) {
                    if (responseData.success) {
                        responseData.data.forEach(function(item) {
                            return item['desc'] = item.abbreviation + (item.subCount > 0 ? ' [' + item.subCount + ']' : '');
                        });
                        return responseData.data;
                    }
                    return [];
                }
            },
            search: {
                enable: true,
                type: 'get',
                url:"/login/listDept",
                dataFilter: function(treeId, parentNode, responseData) {
                    if (responseData.success) {
                        responseData.data.forEach(function(item) {
                            return item['desc'] = item.abbreviation;
                        });
                        return responseData.data;
                    }
                    return [];
                }
            }
        });
    });
</script>
</#macro>